/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiran.midlet;

import java.io.IOException;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.lcdui.game.Sprite;
/**
 * // 240 290
 * @author hiran
 */
public class PuzzleCanvas extends Canvas {
    
    PuzzleCell freeCell = new PuzzleCell(3,3);
    PuzzleCell newCell = new PuzzleCell(3,3);

    int cellsize = 50;
    boolean clicked = false;
    Image offScreen = null;
    Graphics imgGraphics;
    int imgSize = 200;
    String image_name = "/ProportaLogo.jpg";
    Image img;

    public PuzzleCanvas() {
        int screenWidth = getWidth();
        int screenHeight = getHeight();
        try {
            img = Image.createImage(image_name);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        imgSize = (screenWidth>screenHeight)? screenHeight : screenWidth;
        imgSize = (img.getWidth()<imgSize)? img.getWidth() : screenWidth;
        cellsize = imgSize/4;
        
        imgSize = cellsize*4;
    }
    protected void paint(Graphics g) {

	if (offScreen == null) {
	    createOffScreenImage();
	}

	if (clicked) {
	    imgGraphics.copyArea(newCell.X * cellsize, newCell.Y * cellsize, cellsize, cellsize, freeCell.X * cellsize, freeCell.Y * cellsize, g.TOP | g.LEFT);
	    g.drawImage(offScreen, 0, 0, g.TOP | g.LEFT);
	    freeCell.X = newCell.X;
	    freeCell.Y = newCell.Y;
	    clicked = false;
	} else {
	    //Will be executed at the initial stage
	    g.setColor(255, 255, 255);
	    g.fillRect(0, 0, getWidth(), getHeight());
	    g.setColor(0, 0, 0);
	    g.drawImage(offScreen, 0, 0, g.TOP | g.LEFT);

	}
	drowSkeleton(g);
	g.fillRect(freeCell.X * cellsize, freeCell.Y * cellsize, cellsize, cellsize);
    }

    private void createOffScreenImage()
    {
	try {

	    offScreen = Image.createImage(imgSize, imgSize);
	    imgGraphics = offScreen.getGraphics();
	    //Image img = Image.createImage(image_name);
	    imgGraphics.drawRegion(img, 0, 0, 200, 200, Sprite.TRANS_NONE, 0, 0, Graphics.TOP | Graphics.LEFT);
	    imgGraphics.setColor(0, 0, 0);

	    //Rendering the count as well to be easy
//	    for (int i = 0; i < 15; i++) {
//		int row = (i / 4);
//		int column = (i % 4);
//
//		imgGraphics.drawString((new Integer(i + 1)).toString(), column * cellsize + (cellsize/2), row * cellsize + (cellsize/2), Graphics.TOP | Graphics.LEFT);
//	    }
	} catch (Exception ioe) {
	    System.err.println("Problem loading image " + ioe);
	}
	randomizeOffScreenImage();
    }

    public void randomizeOffScreenImage()
    {
	int[] clickArr = {1,1,3,3,2,3,1,1,4,4,4,2,1,2,3,4,2,3,2,4,1,1,3,3,3,3,2,4,4,4,4,3,2,4};
	for (int i=0;i<clickArr.length;i++)
	{
	    switch(clickArr[i])
	    {
		case 1:
		    clickLeft();
		    break;
		case 2:
		    clickRight();
		    break;
		case 3:
		    clickUp();
		    break;
		case 4:
		    clickDown();
		    break;
	    }
	    imgGraphics.copyArea(newCell.X * cellsize, newCell.Y * cellsize, cellsize, cellsize, freeCell.X * cellsize, freeCell.Y * cellsize, Graphics.TOP | Graphics.LEFT);

	    freeCell.X = newCell.X;
	    freeCell.Y = newCell.Y;
	}
	
    }

    private void drowSkeleton(Graphics g)
    {
	for (int i = 0; i <= imgSize; i += cellsize) {
	    g.drawLine(i, 0, i, imgSize);
	    g.drawLine(0, i, imgSize, i);
	}
    }
    private void clickLeft()
    {
	newCell.X = freeCell.X > 0 ? freeCell.X - 1 : freeCell.X;
    }
    private void clickRight()
    {
	newCell.X = freeCell.X < 3 ? freeCell.X + 1 : freeCell.X;
    }
    private void clickUp()
    {
	newCell.Y = freeCell.Y > 0 ? freeCell.Y - 1 : freeCell.Y;
    }
    private void clickDown()
    {
	newCell.Y = freeCell.Y < 3 ? freeCell.Y + 1 : freeCell.Y;
    }
    public void keyPressed(int keyCode) {
	int key = getGameAction(keyCode);
	switch (key) {
	    case LEFT:
		clickLeft();
		clicked = true;
		break;
	    case RIGHT:
		clickRight();
		clicked = true;
		break;
	    case UP:
		clickUp();
		clicked = true;
		break;
	    case DOWN:
		clickDown();
		clicked = true;
		break;
	    case FIRE:

		break;
	}
	repaint();
    }
    
    public void randomized()
    {
        Random r = new Random();
        int i;
        
        for (int j=0;j<100 ;j++)
        {
            i = r.nextInt(4);
            System.out.println(i);
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            keyPressed(-1 * (i+1));
        }
    }

    public void randomized(int i)
    {
        keyPressed(-1 * (i+1));
        serviceRepaints();
    }
}
